#!/usr/bin/env python
# -*- coding: utf-8 -*-

from BeautifulSoup import BeautifulSoup
import urllib2, urllib, cookielib
#import re as reg
"""
class DataError(Exception):
    pass
def GetData(userid, password):
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()), )
    urllib2.install_opener(opener)
    url = "http://jw2005.scuteo.com/"
    res = urllib2.urlopen(url)
    url = res.geturl()
    res = res.read()
    soup = BeautifulSoup(res)
    re = soup.find("input", {"name" : "__VIEWSTATE"})
    data = {}
    data["TextBox1"] = userid
    data["TextBox2"] = password
    data["RadioButtonList1"] = u"学生".encode("gbk")
    data["__VIEWSTATE"] = re["value"]
    data["Button1"] = ""
    data["lbLanguage"] = ""
    postdata = urllib.urlencode(data)
    res = urllib2.urlopen(url, postdata)
    res = res.read().decode("gbk")
    pattern = reg.compile(r"alert\('(.*?)'\)", reg.DOTALL)
    match = pattern.search(res)
    if match:
        raise DataError(match.group(1))
    soup = BeautifulSoup(res)
    re = soup.find("a", onclick = u"GetMc('成绩查询');")
    idname = soup.find("span", id = "xhxm").string

    head = {"Host":"jw2005.scuteo.com",
            "User-Agent":"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Firefox/3.6.15",
            "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language":"zh-cn,zh;q=0.5",
            "Accept-Encoding":"gzip,deflate",
            "Accept-Charset":"GB2312,utf-8;q=0.7,*;q=0.7",
            "Keep-Alive":"115",
            "Connection":"keep-alive",
            "Origin": 'http://jw2005.scuteo.com',
            "Referer": url.encode("gb2312"),
            "Content-Type": "application/x-www-form-urlencoded"}

    urlbase = url[:-13].encode("gb2312")
    url = urlbase + re["href"].encode("gb2312")

    req = urllib2.Request(url, headers = head)
    res = urllib2.urlopen(req)
    soup = BeautifulSoup(res.read())
    data = {}
    re = soup.find("form", id = "Form1")
    head["Referer"] = url
    url = urlbase + re["action"].encode("gb2312")

    data["__VIEWSTATE"] = re.find("input", {"name":"__VIEWSTATE"})["value"]
    data["__EVENTTARGET"] = ''
    data["__EVENTARGUMENT"] = ''
    data["hidLanguage"] = ''
    data["ddl_kcxz"] = ''
    data["ddlXN"] = ""
    data["ddlXQ"] = ""
    data["btn_zcj"] = u"历年成绩".encode("gb2312")

    postdata = urllib.urlencode(data)
    req = urllib2.Request(url, postdata, head)
    res = urllib2.urlopen(req)
    soup = BeautifulSoup(res.read())

    re = soup.find("table", {"class":"datelist"})
    rawcolumns = re.tr.contents
    columns = []
    for rawcolumn in rawcolumns[1:-1]:
        string = rawcolumn.string or rawcolumn.a.string
        columns.append(string)
    items = []
    for item in re.contents[2:-1]:
        subitems = []
        for subitem in item.contents[1:-1]:
            string = subitem.string or ''
            subitems.append(string.replace(u"&nbsp;",u" "))
        items.append(subitems)

    return (columns, items, idname)
"""
print "Content-type: text/html"
print
print "Hello"
#print str(GetData("200930242208","19910216"))
